package bb.lanxing.activity.bluetooth

import android.os.Build
import android.os.Bundle
import android.widget.RadioGroup
import androidx.annotation.RequiresApi
import bb.lanxing.R
import bb.lanxing.activity.base.BaseDisplayActivity
import bb.lanxing.calc.data.DisplayPoint
import bb.lanxing.databinding.ActivityHeartRateModeBinding
import bb.lanxing.devices.utils.XZDeviceHelper
import bb.lanxing.lib.devices.ble.device.HeartRate01Device

class HeartRateModeActivity : BaseDisplayActivity() {


    private lateinit var binding: ActivityHeartRateModeBinding


    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(bundle: Bundle?) {
        super.onCreate(bundle)
        binding = ActivityHeartRateModeBinding.inflate(layoutInflater)

        setContentView(binding.root)
        setupActionBar(true)
        val mode = intent.getIntExtra(HeartRate01Device.EXTRA_HEART_RATE_MODE, 0)
        when (mode) {
            0 -> {
                binding.radioButtonCommon.isChecked = true
            }

            1 -> {
                binding.radioButtonRider.isChecked = true
            }

            2 -> {
                binding.radioButtonWalk.isChecked = true
            }

            3 -> {
                binding.radioButtonFitness.isChecked = true
            }
        }
        binding.radioGroup.setOnCheckedChangeListener(SwitchHeartRateModeListener())
    }

    override fun onRefreshUI(displayPoint: DisplayPoint?) {

    }


    inner class SwitchHeartRateModeListener internal constructor() :
        RadioGroup.OnCheckedChangeListener {
        override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
            when (checkedId) {
                R.id.radio_button_common -> {
                    XZDeviceHelper.getHeartRate01Controller().switchTo(0)
                }

                R.id.radio_button_rider -> {
                    XZDeviceHelper.getHeartRate01Controller().switchTo(1)
                }

                R.id.radio_button_walk -> {
                    XZDeviceHelper.getHeartRate01Controller().switchTo(2)
                }

                R.id.radio_button_fitness -> {
                    XZDeviceHelper.getHeartRate01Controller().switchTo(3)
                }
            }
            binding.radioGroup.postDelayed({
                XZDeviceHelper.getHeartRate01Controller().readCurrentMode()
            }, 1000)
        }
    }

}